/* 
 * Copyright 2008 Rodrigo Reyes <reyes.rr at gmail dot com>
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you
 * may not use this file except in compliance with the License. You may
 * obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
 * implied. See the License for the specific language governing
 * permissions and limitations under the License.
**/
package net.kornr.abstractcanvas.client.gwt;

import java.util.Iterator;
import java.util.LinkedList;

import net.kornr.abstractcanvas.client.CanvasPainter;
import net.kornr.abstractcanvas.client.CanvasTextSupport;
import net.kornr.abstractcanvas.client.ICanvasExt;
import net.kornr.abstractcanvas.client.Rectangle;
import net.kornr.abstractcanvas.client.TextMetrics;


import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.widgetideas.graphics.client.Color;

public class CanvasPanelExt extends CanvasPanel implements ICanvasExt 
{
	private LinkedList<CanvasPainter> m_controllers = new LinkedList<CanvasPainter>();
	private String m_fontname = "Serif";
	private int m_fontsize = 12;
	private int m_align = CanvasTextSupport.ALIGN_START;
	
	public static class Text
	{
		public int x,y;
		public Label text;
	}
	
	private LinkedList<HTML> m_texts = new LinkedList<HTML>(); 
	private HTML m_textmetrics = new HTML();
	
	public CanvasPanelExt()
	{
		this(300,150);
	}
	
	public CanvasPanelExt(int width, int height) {
		super(width, height);
		this.add(m_textmetrics, 0, 0);
	}

	public void clear() 
	{
		super.clear();

		for (Iterator<HTML>i=m_texts.iterator(); i.hasNext(); )
		{
			this.remove(i.next());
		}
		m_texts.clear();
	}
	
	public void strokeText(String text, int x, int y)
	{
		String val = "<span style=\"font-family: " + m_fontname+"; color: " + m_canvas.getStrokeStyleColor().toString()+"; font-size: "+m_fontsize+"px;\">"+text+"</span>";
		AlignedHtml h = new AlignedHtml(val, this, m_align);
		Text t = new Text();
		t.x = x;
		t.y = y;
		m_texts.add(h);
		this.add(h,x,y);
		this.setWidgetPosition(h, x - h.getElement().getOffsetWidth(), y);
	}

	public TextMetrics measureText(String text) 
	{
		m_textmetrics.setHTML("<div style='font-size: " + m_fontsize + "px; font-family: " + m_fontname +";'>"+text+"</div>");
		m_textmetrics.setVisible(true);
		double w = m_textmetrics.getOffsetWidth();
		double h = m_textmetrics.getOffsetHeight();
		m_textmetrics.setVisible(false);

		return new TextMetrics(w,h);
	}

	public void addCanvasPainter(CanvasPainter controller) 
	{
		m_controllers.add(controller);
	}

	public void removeCanvasPainter(CanvasPainter controller) 
	{
		m_controllers.remove(controller);
	}

	@Override
	protected void onLoad() {
		super.onLoad();
		for (Iterator<CanvasPainter>i=m_controllers.iterator(); i.hasNext(); )
		{
			CanvasPainter ctrl = i.next();
			ctrl.drawCanvas(this);
		}
	}

	public void setFontName(String fontname)
	{
		m_fontname = fontname;
	}
	
	public void setFontSize(int pixelfontsize)
	{
		m_fontsize = pixelfontsize;
	}

	public void setAlign(int align) 
	{
		m_align = align;
	}

}
